package com.cloudy.security;

import java.security.SignatureException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import static com.cloudy.impl.Constants.*;

public class RFC2104HMAC {

	public static String calculateRFC2104HMAC(String data, String key)
			throws java.security.SignatureException {
		String result = null;
		try {
	
			SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(),
					SIGNATURE_METHOD);
			Mac mac = Mac.getInstance(SIGNATURE_METHOD);
			mac.init(signingKey);
	
			byte[] rawSignature = mac.doFinal(data.getBytes());
	
			result = Base64.encode(rawSignature);
			result = result.trim();
	
		} catch (Exception e) {
			throw new SignatureException("Failed to generate HMAC : "
					+ e.getMessage());
		}
		return result;
	}

}
